package com.th.controller;

import com.th.common.enums.TransactionType;
import com.th.common.response.CommonResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.th.common.domain.UsSessionToken;
import com.th.common.domain.UsWalletInfo;
import com.th.common.domain.UsWalletTransaction;
import com.th.common.utils.DateUtils;
import com.th.common.vo.WalletInfoVo;
import com.th.config.web.BaseController;
import com.th.service.UsSessionTokenService;
import com.th.service.UsWalletInfoService;
import com.th.service.UsWalletTransactionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.checkerframework.checker.units.qual.A;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author 第五组
 * @Description TODO
 * @createTime 2023-06-24 15:24
 */

@RestController
@RequestMapping("/api/payment")
@Api(tags = "用户支付接口")
public class UsPaymentController extends BaseController {

    @Resource
    private UsWalletInfoService usWalletInfoService;
    @Resource
    private UsWalletTransactionService usWalletTransactionService;

    @GetMapping("/getWalletInfo")
    @ApiOperation("获取用户钱包基本信息")
    public WalletInfoVo getWalletInfo(@RequestHeader("token") String token){
        WalletInfoVo wallerInfoVo = usWalletInfoService.getWalletInfoVo();
        return wallerInfoVo;
    }

    /***
     * 充值提现 交易处理
     * @param token
     * @param paymentMethod
     * @param type
     * @param amount
     * @return
     */
    @ApiOperation("充值提现处理")
    @PostMapping("/dealWallet")
    public CommonResponse<String> dealWallet(@RequestHeader("token") String token, @RequestParam("paymentMethod") Integer paymentMethod, @RequestParam("type") Integer type, @RequestParam("amount") Integer amount) {
        //status 判断 0提现 1充值
        if (type == 0) {
            return CommonResponse.success(usWalletTransactionService.Withdraw(paymentMethod,amount));
        } else if (type == 1) {
            if(paymentMethod==0){
                //排除非法传参
                return CommonResponse.error(404,"系统错误！");
            }
            return CommonResponse.success(usWalletTransactionService.Recharge(paymentMethod,amount));
        }else {
            //排除非法传参
            return CommonResponse.error(404,"系统错误！");
        }
    }

    /***
     * 分页查询
     * @param token
     * @param pageSize
     * @param page
     * @return
     */

    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Page<UsWalletTransaction> getUsWalletTransaction(@RequestHeader("token") String token, int pageSize, int page){
        Page<UsWalletTransaction> pageInfo = new Page<>(page,pageSize);
        usWalletTransactionService.page(pageInfo,usWalletTransactionService.getUsWalletTransaction());
        return pageInfo;
    }


    /***
     * 根据日期区间查询账单
     * @param token
     * @param startTime
     * @param EndTime
     * @param pageSize
     * @param page
     * @param type
     * @return
     */
    @ApiOperation("根据日期区间查询账单")
    @GetMapping("/accountBytime")
    public Page<UsWalletTransaction> getAccountBytime(@RequestHeader("token") String token, Date startTime, Date EndTime, Integer pageSize, Integer page, Integer type){
        Page<UsWalletTransaction> pageInfo = new Page<>(page,pageSize);
        usWalletTransactionService.page(pageInfo,usWalletTransactionService.getUsWalletTransactionCompareByTime(startTime,EndTime,type));
        return pageInfo;
    }

    /***
     * 当月支出
     * @param token
     * @return
     */
    @ApiOperation("当月支出")
    @GetMapping("/currentMonthExpenditure")
    public CommonResponse<String> getCurrentMonthExpenditure(@RequestHeader("token") String token){
        //获取当前时间
        Date nowTime= DateUtils.getNowDate();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(nowTime);
        //获取月份和年份
        Integer month = calendar.get(Calendar.MONTH) + 1; //获取的月份需要加1
        Integer year=calendar.get(Calendar.YEAR);
       return CommonResponse.success(usWalletTransactionService.getCurrentMonthExpenditure(month,year));
    }

    /***
     * 当月收入
     * @param token
     * @return
     */
    @ApiOperation("当月收入")
    @GetMapping("/currentMonthIncome")
    public CommonResponse<String> getCurrentMonthIncome(@RequestHeader("token") String token){
        //获取当前时间
        Date nowTime= DateUtils.getNowDate();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(nowTime);
        //获取月份和年份
        Integer month = calendar.get(Calendar.MONTH) + 1;
        Integer year=calendar.get(Calendar.YEAR);
        return CommonResponse.success(usWalletTransactionService.getCurrentMonthIncome(month,year));
    }

    /***
     * 收入支出查询
     * @param token
     * @param pageSize
     * @param page
     * @param type
     * @return
     */
    @ApiOperation("收入支出查询")
    @GetMapping("/getIncomeAndExpenditure")
    public Page<UsWalletTransaction> getIncomeAndExpenditure(@RequestHeader("token") String token, @RequestParam("pageSize") Integer pageSize, @RequestParam("page") Integer page,@RequestParam("type") Integer type){
        Page<UsWalletTransaction> pageInfo = new Page<>(page,pageSize);
        usWalletTransactionService.page(pageInfo,usWalletTransactionService.getIncomeAndExpenditure(type));
        return pageInfo;
    }

}
